package com.szd.qiongyou20.home.activity;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.szd.qiongyou20.R;
import com.szd.qiongyou20.home.bean.KeywordBus;
import com.szd.qiongyou20.home.search.result_fragment.Result_Fragment;
import com.szd.qiongyou20.home.search.result_fragment.Select_Fragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

public class SearchActivity extends AppCompatActivity {

    private Context mContext = this;
    private Toolbar mToolbar;
    private SearchView mSearchView;
    private ImageView mImageView;
    private TextView mTextView;
    public static String KeyWord;


    private FragmentManager mFragmentManager;
    private FragmentTransaction mTransaction;

    private Select_Fragment mSelect_fragment;
    private Result_Fragment mResult_fragment;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        mFragmentManager = getSupportFragmentManager();
        initView();

        EventBus.getDefault().register(mContext);
    }

    private void initView() {

        // 设置顶部的ToolBar
        mToolbar = (Toolbar) findViewById(R.id.toolBar_search);
        setSupportActionBar(mToolbar);

        // 顶部的返回按钮
        mImageView = (ImageView) findViewById(R.id.imageView_search_back);

        // 初始化SearchView的初始状态
        // 创建提示文本的对象
        SpannableString spanText = new SpannableString("搜索目的地、锦囊、游记、特价、穷游er");
        // 设置默认搜索文本的颜色
        spanText.setSpan(new ForegroundColorSpan(Color.WHITE), 0,
                spanText.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

        mSearchView = (SearchView) findViewById(R.id.searchView_);
        mSearchView.setQueryHint(spanText);
        mSearchView.setIconified(false);
        mSearchView.setIconifiedByDefault(false);

        // 获取获取SearchView上的
        mTextView = (TextView) mSearchView
                .findViewById(android.support.v7.appcompat.R.id.search_src_text);
        mTextView.setTextColor(Color.WHITE);

        // 设置关闭按钮的样式
        ImageView closeButton = (ImageView) mSearchView
                .findViewById(android.support.v7.appcompat.R.id.search_close_btn);
        closeButton.setImageResource(R.mipmap.ic_close_trans);

        // 顶部返回箭头的点击事件
        onBackClick();
        // SearchView的搜索事件
        onSearch();
        // 关闭按钮的点击事件
        closeButtonOnClick(closeButton);


        mTransaction = mFragmentManager.beginTransaction();
        mSelect_fragment = new Select_Fragment();
        // 添加最开始的选择界面
        mTransaction.add(R.id.frameLayout_Search, mSelect_fragment);
        mTransaction.commit();
    }

    @Subscribe
    public void resultFragment(KeywordBus keyword) {
        mTransaction = mFragmentManager.beginTransaction();
        mResult_fragment = Result_Fragment.newInstance(keyword.getKeyword());
        mTransaction.replace(R.id.frameLayout_Search, mResult_fragment);
        mTransaction.commit();
        mSearchView.setQuery(keyword.getKeyword(), true);
    }


    /**
     * 搜索的方法
     */
    private void onSearch() {
        mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            /**
             * 点击搜索按钮时回调的方法
             * @param query 要搜索的文本
             * @return
             */
            @Override
            public boolean onQueryTextSubmit(String query) {
                mTransaction = mFragmentManager.beginTransaction();
                if (query.equals("")) {
                    Toast.makeText(mContext, "输入为空!", Toast.LENGTH_SHORT).show();
                    return false;
                }
                mResult_fragment = Result_Fragment.newInstance(query);
                mTransaction.replace(R.id.frameLayout_Search, mResult_fragment);
                mTransaction.commit();
                return true;
            }

            /**
             * 搜索的文本改变时回调的方法
             * @param newText 新的文本
             * @return
             */
            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
    }

    /**
     * 点击
     * @param closeButton searchView中的X号的对象
     */
    private void closeButtonOnClick(ImageView closeButton) {
        closeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTransaction = mFragmentManager.beginTransaction();
                // 设置输入的文本为空 返回至提示内容
                mTextView.setText("");
                mTransaction.replace(R.id.frameLayout_Search, mSelect_fragment);
                mTransaction.commit();
            }
        });
    }

    private void onBackClick() {
        mImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    /**
     * 系统返回键的点击事件
     */
    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(mContext);
    }
}
